package com.chenxi.common.controller;

import com.chenxi.constant.CommonConstants;
import com.chenxi.util.ResultJson;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;

@Slf4j
public class BaseController {

    public static ResultJson getError(){
        ResultJson r = new ResultJson();

        r.error();
        return r;
    }

    public static ResultJson getSuccess(){
        ResultJson r = new ResultJson();
        r.success();
        return r;
    }

    /**
     * Description：<br>
     * 获取分页对象
     * @author  Simon.Hoo
     * @date    2015年9月22日
     * @version v1.0.0
     * @param request
     * @return
     * @throws
     */
    protected PageInfo getPageInfo(HttpServletRequest request){
        String pageJson = request.getParameter(CommonConstants.REQUEST_PAGE);
        if(pageJson==null){
            return null;
        }
        Gson gson = new Gson();
        PageInfo pageInfo = new PageInfo();
        try {
            if (!StringUtils.isEmpty(pageJson)) {
                pageInfo = (PageInfo) gson.fromJson(pageJson, PageInfo.class);
            }else{
                String page = request.getParameter(CommonConstants.REQUEST_PAGE);
                String pageSize = request.getParameter(CommonConstants.REQUEST_PAGE_SIZE);
                pageInfo.setPageSize(StringUtils.isEmpty(pageSize) ? 0 : Integer.parseInt(pageSize));
                pageInfo.setPages(StringUtils.isEmpty(page) ? 15 : Integer.parseInt(page));
            }
        } catch (Exception e){
            if (log.isDebugEnabled()) {
                e.printStackTrace();
            }
        }

        return pageInfo;
    }
}
